package com.woniuxy.yoga.course.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.coach.dao.model.Coach;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.BusinessState;
import com.woniuxy.yoga.commons.core.statedata.CoachState;
import com.woniuxy.yoga.commons.core.statedata.CourseState;
import com.woniuxy.yoga.commons.core.statedata.ProductState;
import com.woniuxy.yoga.commons.core.util.DateUtil;
import com.woniuxy.yoga.course.client.BusinessClient;
import com.woniuxy.yoga.course.client.CoachClient;
import com.woniuxy.yoga.course.client.CourseClient;
import com.woniuxy.yoga.course.client.ProductClient;
import com.woniuxy.yoga.course.client.dto.business.BusinessStoreDTO;
import com.woniuxy.yoga.course.client.dto.business.StoreClassroomByIdDTO;
import com.woniuxy.yoga.course.client.dto.product.AllTeamProductDTO;
import com.woniuxy.yoga.course.dao.model.TeamCourse;
import com.woniuxy.yoga.course.dao.mapper.TeamCourseMapper;
import com.woniuxy.yoga.course.model.dto.alone.CoachTeamCourseDTO;
import com.woniuxy.yoga.course.model.dto.ordinary.OrdinaryCourseDTO;
import com.woniuxy.yoga.course.model.dto.team.AllTeamCourseDTO;
import com.woniuxy.yoga.course.model.dto.team.TeamCourseDTO;
import com.woniuxy.yoga.course.model.exception.CourseException;
import com.woniuxy.yoga.course.model.exception.CourseExceptionCode;
import com.woniuxy.yoga.course.model.param.team.AddTeamCourseParam;
import com.woniuxy.yoga.course.model.param.team.AmountTeamCourseDurationParam;
import com.woniuxy.yoga.course.model.param.team.CoachViewTeamCourseParam;
import com.woniuxy.yoga.course.model.param.team.QueryAllTeamCourseParam;
import com.woniuxy.yoga.course.service.service.TeamCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.yoga.product.model.dto.TeamProductByIdDTO;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 团课课程表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class TeamCourseServiceImpl extends ServiceImpl<TeamCourseMapper, TeamCourse> implements TeamCourseService {

    @Autowired
    private TeamCourseMapper teamCourseMapper;

    @Autowired
    private ProductClient productClient;

    @Autowired
    private BusinessClient businessClient;

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private CoachClient coachClient;

    /**
     * 教练ID查询团队课程
     * @param ids
     * @return
     */
    @Override
    public List<TeamCourseDTO> getByCoachId(List<Integer> ids) {
        QueryWrapper<TeamCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",ids.get(0));
        //索引排序
        wrapper.orderByAsc("team_course_id");
        //获取数据
        List<TeamCourse> teamCourses = teamCourseMapper.selectList(wrapper);
        //数据转换
        List<TeamCourseDTO> teamCourseDTOS = BeanUtil.copyToList(teamCourses, TeamCourseDTO.class);
        return teamCourseDTOS;
    }

    /**
     * 新增团队课程
     * @param param
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void addTeamCourse(AddTeamCourseParam param) {
        //判断团课产品是否报错
        Result<TeamProductByIdDTO> teamProductByIdDTOResult = productClient.getTeamProductById(param.getTeamProductId());
        if (teamProductByIdDTOResult.getCode()!=200) throw new CourseException(teamProductByIdDTOResult.getCode(),teamProductByIdDTOResult.getMessage());
        //获取数据
        TeamProductByIdDTO teamProductByIdDTO = teamProductByIdDTOResult.getData();
        //判断是否存在
        if (teamProductByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_PRODUCT_NOT_EXIST);
        //判断团队产品状态是否异常
        if (teamProductByIdDTO.getTeamProductState()!= ProductState.TEAM_PRODUCT_STATE_WAIT) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_PRODUCT_STATE_ERROR);
        //判断商家是否报错
        Result<BusinessStoreDTO> businessStoreDTOResult = businessClient.selByBusinessStoreId(param.getStoreId());
        if (businessStoreDTOResult.getCode()!=200) throw new CourseException(businessStoreDTOResult.getCode(),businessStoreDTOResult.getMessage());
        //获取数据
        BusinessStoreDTO businessStoreDTO = businessStoreDTOResult.getData();
        //判断是否存在
        if (businessStoreDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_BUSINESS_STORE_NOT_EXIST);
        //判断场馆状态是否异常
        if (businessStoreDTO.getStoreState()== BusinessState.STORE_STATE_DELETE) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_BUSINESS_STORE_STATE_ERROR);
        //判断教练是否报错
        Result<Coach> coachResult = coachClient.getCoachById(param.getCoachId());
        if (coachResult.getCode()!=200) throw new CourseException(coachResult.getCode(),coachResult.getMessage());
        //获取数据
        Coach coach = coachResult.getData();
        //判断是否存在
        if (coach==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_NOT_EXIST);
        //判断教练状态是否异常
        if (coach.getCoachState()!= CoachState.COACH_STATE_NORMAL) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_STATE_ERROR);
        //获取开始时间
        String startTime = param.getStartTime();
        long start = DateUtil.convertToMilliseconds(startTime);
        System.out.println(startTime);
        //获取结束时间 需要加两个小时
        String endTime = param.getEndTime();
        String newEndTime = DateUtil.increaseTwoHour(endTime);
        long end = DateUtil.convertToMilliseconds(newEndTime);
        System.out.println(endTime);
        //准备一个保存教练ID的集合
        List<Integer> ids = new ArrayList<>();
        ids.add(param.getCoachId());
        //查询该教练的所有普通课程 判断时间是否存在重复 需要注意课程状态
        Result<List<OrdinaryCourseDTO>> ordinaryCourseDTOResult = courseClient.getOrdinaryByCoachId(ids);
        //判断是否报错
        if (ordinaryCourseDTOResult.getCode()!=200) throw new CourseException(ordinaryCourseDTOResult.getCode(),ordinaryCourseDTOResult.getMessage());
        //获取数据
        List<OrdinaryCourseDTO> ordinaryCourseDTOS = ordinaryCourseDTOResult.getData();
        //判断是否存在数据
        if (ordinaryCourseDTOS!=null&&ordinaryCourseDTOS.size()>0){
            //遍历该教练的所有的普通课程
            for (OrdinaryCourseDTO ordinaryCourseDTO : ordinaryCourseDTOS) {
                //判断状态为待开课和已开课的普通课程信息
                if (ordinaryCourseDTO.getCourseState()== CourseState.ORDINARY_COURSE_STATE_WAIT || ordinaryCourseDTO.getCourseState()== CourseState.ORDINARY_COURSE_STATE_START){
                    //获取普通课程开始时间
                    String courseStartDate = ordinaryCourseDTO.getCourseStartDate();
                    long courseStartTime = DateUtil.convertToMilliseconds(courseStartDate);
                    //获取普通课程结束时间
                    String courseEndDate = ordinaryCourseDTO.getCourseEndDate();
                    long courseEndTime = DateUtil.convertToMilliseconds(courseEndDate);
                    //判断新增的团队课程时间是否存在冲突
                    if (!((courseStartTime<start&&courseEndTime<start) || (courseStartTime>end&&courseEndTime>end))){
                        //存在 教练冲突
                        throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_TIME_CONFLICT);
                    }
                }
            }
        }
        //查询该教练的所有团队产品 判断时间是否存在重复 需要注意产品状态
        Result<List<AllTeamProductDTO>> allTeamProductDTOResult = productClient.getProductByCoachId(ids);
        //判断是否报错
        if (allTeamProductDTOResult.getCode()!=200) throw new CourseException(allTeamProductDTOResult.getCode(),allTeamProductDTOResult.getMessage());
        //获取数据
        List<AllTeamProductDTO> allTeamProductDTOS = allTeamProductDTOResult.getData();
        //判断是否存在数据
        if (allTeamProductDTOS!=null&&allTeamProductDTOS.size()>0){
            //遍历该教练的所有团队产品
            for (AllTeamProductDTO allTeamProductDTO : allTeamProductDTOS) {
                //判断团队产品状态是否是待开课或已开课
                if (allTeamProductDTO.getTeamProductState()==ProductState.TEAM_PRODUCT_STATE_WAIT || allTeamProductDTO.getTeamProductState()==ProductState.TEAM_PRODUCT_STATE_START){
                    //先排除掉自己当前的产品
                    if (allTeamProductDTO.getTeamProductId()!=param.getTeamProductId()){
                        //获取团队产品开始时间
                        String teamStartTime = allTeamProductDTO.getOpenTime();
                        long teamOneTime = DateUtil.convertToMilliseconds(teamStartTime);
                        //获取团队产品结束时间 加两个小时
                        String oldTeamEndTime = DateUtil.addDaysToString(allTeamProductDTO.getOpenTime(), allTeamProductDTO.getHourCount()-1);
                        String teamEndTime = DateUtil.increaseTwoHour(oldTeamEndTime);
                        long teamTwoTime = DateUtil.convertToMilliseconds(teamEndTime);
                        //判断新增的团队课程时间是否存在冲突
                        if (!((teamOneTime<start&&teamTwoTime<start) || (teamOneTime>end&&teamTwoTime>end))){
                            //存在 教室冲突
                            throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_TIME_CONFLICT);
                        }
                    }
                }
            }
        }
        //判断教室是否报错
        Result<StoreClassroomByIdDTO> storeClassroomByIdDTOResult = businessClient.selNormalByStoreClassroomId(param.getClassroomId());
        if (storeClassroomByIdDTOResult.getCode()!=200) throw new CourseException(storeClassroomByIdDTOResult.getCode(),storeClassroomByIdDTOResult.getMessage());
        //获取数据
        StoreClassroomByIdDTO storeClassroomByIdDTO = storeClassroomByIdDTOResult.getData();
        //判断是否存在
        if (storeClassroomByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_NOT_EXIST);
        //判断教室状态是否异常
        if (storeClassroomByIdDTO.getClassroomState()!=BusinessState.CLASSROOM_STATE_NORMAL) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_STATE_ERROR);
        //判断该教室是否是团队教室
        if (storeClassroomByIdDTO.getClassroomType()==0) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_NO_TEAM_CLASS_ROOM);
        //查询该教室的所有团队课程 判断未来时间是否存在重复 需要主要课程状态(只有一个团课教室)
        QueryWrapper<TeamCourse> wrapper = new QueryWrapper<>();
        //将当前时间转换成字符串 查询当前时间之后的团队课程
        wrapper.gt("start_time",DateUtil.dateTime2String(new Date()));
        //当前门店的团队课程
        wrapper.eq("store_id",param.getStoreId());
        //查询数据
        List<TeamCourse> teamCourses = teamCourseMapper.selectList(wrapper);
        //判断是否存在
        if (teamCourses!=null&&teamCourses.size()>0){
            //遍历当前时间之后的该教室的所有团队课程
            for (TeamCourse teamCours : teamCourses) {
                //不需判断团队课程状态 未来时间的课程状态都是待开课
                //获取每天团队课程的开课时间
                String startTimeOne = teamCours.getStartTime();
                long teamOne = DateUtil.convertToMilliseconds(startTimeOne);
                //获取每天团队课程的结束时间
                String endTimeTwo = teamCours.getEndTime();
                long teamTwo = DateUtil.convertToMilliseconds(endTimeTwo);
                //判断新增的团队课程时间中是否存在冲突
                if (!((teamOne<start&&teamTwo<start) || (teamOne>end&&teamTwo>end))){
                    //存在 教室时间冲突
                    throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_TIME_CONFLICT);
                }
            }
        }
        //判断该教练是否属于该场馆
        if (coach.getStoreId()!=param.getStoreId()) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_NOT_BELONG_STORE);
        //判断该教室是否属于该场馆
        if (storeClassroomByIdDTO.getStoreId()!=param.getStoreId()) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_NOT_BELONG_STORE);
        //通过开始时间和结束时间获取天数
        Integer days = DateUtil.stringDateTime2Days(param.getStartTime(), param.getEndTime());
        //循环天数添加课程
        for (Integer i = 0; i < days; i++) {
            //对象转换
            TeamCourse teamCourse = BeanUtil.toBean(param, TeamCourse.class);
            //每次循环时间天数加一
            String newStartTime = DateUtil.addDaysToString(param.getStartTime(), i);
            //赋值开始时间
            teamCourse.setStartTime(newStartTime);
            //赋值结束时间 加两个小时
            teamCourse.setEndTime(DateUtil.increaseTwoHour(newStartTime));
            //团队课程状态设置初始值为待开课
            teamCourse.setTeamCourseState(CourseState.TEAM_COURSE_STATE_WAIT);
            //新增数据
            teamCourseMapper.insert(teamCourse);
        }
    }

    /**
     * 修改成进行中
     * @param teamCourseId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyTeamCourseHave(Integer teamCourseId) {
        TeamCourse teamCourse = teamCourseMapper.selectById(teamCourseId);
        //判断团队课程是否存在
        if (teamCourse==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_COURSE_NOT_EXIST);
        //判断团队课程状态是否异常
        if (teamCourse.getTeamCourseState()!=CourseState.TEAM_COURSE_STATE_WAIT) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_COURSE_STATE_ERROR);
        //判断团课产品是否报错
        Result<TeamProductByIdDTO> teamProductByIdDTOResult = productClient.getTeamProductById(teamCourse.getTeamProductId());
        if (teamProductByIdDTOResult.getCode()!=200) throw new CourseException(teamProductByIdDTOResult.getCode(),teamProductByIdDTOResult.getMessage());
        //获取数据
        TeamProductByIdDTO teamProductByIdDTO = teamProductByIdDTOResult.getData();
        //判断是否存在
        if (teamProductByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_PRODUCT_NOT_EXIST);
        //判断团队产品状态是否异常(是否是已开课)
        if (teamProductByIdDTO.getTeamProductState()!=ProductState.TEAM_PRODUCT_STATE_START) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_PRODUCT_STATE_ERROR);
        //判断场馆是否报错
        Result<BusinessStoreDTO> businessStoreDTOResult = businessClient.selByBusinessStoreId(teamCourse.getStoreId());
        if (businessStoreDTOResult.getCode()!=200) throw new CourseException(businessStoreDTOResult.getCode(),businessStoreDTOResult.getMessage());
        //获取数据
        BusinessStoreDTO businessStoreDTO = businessStoreDTOResult.getData();
        //判断是否存在
        if (businessStoreDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_BUSINESS_STORE_NOT_EXIST);
        //判断场馆状态是否异常
        if (businessStoreDTO.getStoreState()==BusinessState.STORE_STATE_DELETE) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_BUSINESS_STORE_STATE_ERROR);
        //修改成进行中
        teamCourse.setTeamCourseState(CourseState.TEAM_COURSE_STATE_HAVE);
        teamCourseMapper.updateById(teamCourse);
    }

    /**
     * 修改成已完成(需要将该团队课程对应的团队产品总天数减一,到零的时候将团队产品状态该为已完成)
     * @param teamCourseId
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void modifyTeamCourseComplete(Integer teamCourseId) {
        TeamCourse teamCourse = teamCourseMapper.selectById(teamCourseId);
        //判断团队课程是否存在
        if (teamCourse==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_COURSE_NOT_EXIST);
        //判断团队课程状态是否异常
        if (teamCourse.getTeamCourseState()!=CourseState.TEAM_COURSE_STATE_HAVE) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_COURSE_STATE_ERROR);
        //判断团课产品是否报错
        Result<TeamProductByIdDTO> teamProductByIdDTOResult = productClient.getTeamProductById(teamCourse.getTeamProductId());
        if (teamProductByIdDTOResult.getCode()!=200) throw new CourseException(teamProductByIdDTOResult.getCode(),teamProductByIdDTOResult.getMessage());
        //获取数据
        TeamProductByIdDTO teamProductByIdDTO = teamProductByIdDTOResult.getData();
        //判断是否存在
        if (teamProductByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_PRODUCT_NOT_EXIST);
        //判断团队产品状态是否异常(是否是已开课)
        if (teamProductByIdDTO.getTeamProductState()!=ProductState.TEAM_PRODUCT_STATE_START) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_TEAM_PRODUCT_STATE_ERROR);
        //判断场馆是否报错
        Result<BusinessStoreDTO> businessStoreDTOResult = businessClient.selByBusinessStoreId(teamCourse.getStoreId());
        if (businessStoreDTOResult.getCode()!=200) throw new CourseException(businessStoreDTOResult.getCode(),businessStoreDTOResult.getMessage());
        //获取数据
        BusinessStoreDTO businessStoreDTO = businessStoreDTOResult.getData();
        //判断是否存在
        if (businessStoreDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_BUSINESS_STORE_NOT_EXIST);
        //判断场馆状态是否异常
        if (businessStoreDTO.getStoreState()==BusinessState.STORE_STATE_DELETE) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_BUSINESS_STORE_STATE_ERROR);
        //修改成已完成
        teamCourse.setTeamCourseState(CourseState.TEAM_COURSE_STATE_COMPLETE);
        teamCourseMapper.updateById(teamCourse);
        //将团队产品的总天数减一
        Result<Integer> integerResult = productClient.teamProductHourCountMinusOne(teamCourse.getTeamProductId());
        if (integerResult.getCode()!=200) throw new CourseException(integerResult.getCode(),integerResult.getMessage());
        //获取数据
        Integer num = integerResult.getData();
        //判断是否存在
        if (num==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_DATA_NOT_EXIST);
        //判断数据是否为零
        if (num==0){
            //团队产品课程已完成
            Result<?> result = productClient.teamProductCompleteTeamProduct(teamCourse.getTeamProductId());
            if (result.getCode()!=200) throw new CourseException(result.getCode(),result.getMessage());
        }
    }

    /**
     * 查询所有的课程信息
     * @param param
     * @return
     */
    @Override
    public PageInfo queryAllTeamCourse(QueryAllTeamCourseParam param) {
        QueryWrapper<TeamCourse> wrapper = new QueryWrapper<>();
        //团课产品名搜索
        if (ObjectUtil.isNotEmpty(param.getTeamProductName())){
            wrapper.like("team_product_name",param.getTeamProductName());
        }
        //开始时间搜索
        if (ObjectUtil.isNotEmpty(param.getStartTime())){
            wrapper.ge("start_time",param.getStartTime());
        }
        //结束时间搜索
        if (ObjectUtil.isNotEmpty(param.getEndTime())){
            wrapper.le("end_time",param.getEndTime());
        }
        //课程状态搜索
        if (ObjectUtil.isNotEmpty(param.getTeamCourseState())){
            wrapper.eq("team_course_state",param.getTeamCourseState());
        }
        //绑定门店
        wrapper.eq("store_id",param.getStoreId());
        //索引排序
        wrapper.orderByAsc("team_course_id");
        //分页查询
        Page<TeamCourse> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 10 : param.getPageSize());
        teamCourseMapper.selectPage(page,wrapper);
        //集合转换
        List<AllTeamCourseDTO> allTeamCourseDTOS = new ArrayList<>();
        if (page.getRecords()!=null&&page.getRecords().size()>0){
            for (TeamCourse teamCourse : page.getRecords()) {
                //对象转换
                AllTeamCourseDTO allTeamCourseDTO = BeanUtil.toBean(teamCourse, AllTeamCourseDTO.class);
                //商家名称赋值
                Result<BusinessStoreDTO> businessStoreDTOResult = businessClient.selByBusinessStoreId(teamCourse.getStoreId());
                if (businessStoreDTOResult.getCode()!=200) throw new CourseException(businessStoreDTOResult.getCode(),businessStoreDTOResult.getMessage());
                //获取数据
                BusinessStoreDTO businessStoreDTO = businessStoreDTOResult.getData();
                allTeamCourseDTO.setStoreName(businessStoreDTO.getStoreName());
                //教练名称赋值
                Result<Coach> coachResult = coachClient.getCoachById(teamCourse.getCoachId());
                if (coachResult.getCode()!=200) throw new CourseException(coachResult.getCode(),coachResult.getMessage());
                //获取数据
                Coach coach = coachResult.getData();
                //判断是否存在
                if (coach==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_NOT_EXIST);
                allTeamCourseDTO.setCoachName(coach.getCoachName());
                //教室名称赋值
                Result<StoreClassroomByIdDTO> storeClassroomByIdDTOResult = businessClient.selNormalByStoreClassroomId(teamCourse.getClassroomId());
                if (storeClassroomByIdDTOResult.getCode()!=200) throw new CourseException(storeClassroomByIdDTOResult.getCode(),storeClassroomByIdDTOResult.getMessage());
                //获取数据
                StoreClassroomByIdDTO storeClassroomByIdDTO = storeClassroomByIdDTOResult.getData();
                //判断是否存在
                if (storeClassroomByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_NOT_EXIST);
                allTeamCourseDTO.setClassroomName(storeClassroomByIdDTO.getClassroomName());
                //状态赋值
                allTeamCourseDTO.setTeamCourseState(teamCourse.getTeamCourseState()==0?"待开课":teamCourse.getTeamCourseState()==1?"进行中":"已完成");
                //保存到集合中
                allTeamCourseDTOS.add(allTeamCourseDTO);
            }
        }
        //分页对象转换
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(allTeamCourseDTOS);
        return pageInfo;
    }

    /**
     * 统计该时间段的团队课程已完成的总时长
     * @param param
     * @return
     */
    @Override
    public Integer amountTeamCourseDuration(AmountTeamCourseDurationParam param) {
        QueryWrapper<TeamCourse> wrapper = new QueryWrapper<>();
        wrapper.ge("start_time",param.getStartTime());
        wrapper.le("end_time",param.getEndTime());
        wrapper.eq("store_id",param.getStoreId());
        wrapper.eq("team_course_state",CourseState.TEAM_COURSE_STATE_COMPLETE);
        //查询数据
        List<TeamCourse> teamCourses = teamCourseMapper.selectList(wrapper);
        //准备一个参数用于统计团课总时长
        Integer duration = 0;
        //判断是否存在
        if (teamCourses!=null&&teamCourses.size()>0){
            //遍历 统计时长
            for (TeamCourse teamCours : teamCourses) {
                duration += 2;
            }
        }
        return duration;
    }

    /**
     * 教练查询自己的团队课程
     * @param param
     * @return
     */
    @Override
    public PageInfo coachViewTeamCourse(CoachViewTeamCourseParam param) {
        QueryWrapper<TeamCourse> wrapper = new QueryWrapper<>();
        //该教练的团队课程
        wrapper.eq("coach_id",param.getCoachId());
        //团课产品名搜索
        if (ObjectUtil.isNotEmpty(param.getTeamProductName())){
            wrapper.like("team_product_name",param.getTeamProductName());
        }
        //开始时间搜索
        if (ObjectUtil.isNotEmpty(param.getStartTime())){
            wrapper.ge("start_time",param.getStartTime());
        }
        //结束时间搜索
        if (ObjectUtil.isNotEmpty(param.getEndTime())){
            wrapper.le("end_time",param.getEndTime());
        }
        //课程状态搜索
        if (ObjectUtil.isNotEmpty(param.getTeamCourseState())){
            wrapper.eq("team_course_state",param.getTeamCourseState());
        }
        //索引排序
        wrapper.orderByAsc("team_course_id");
        //分页查询
        Page<TeamCourse> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 10 : param.getPageSize());
        teamCourseMapper.selectPage(page,wrapper);
        //集合转换
        List<CoachTeamCourseDTO> coachTeamCourseDTOS = new ArrayList<>();
        if (page.getRecords()!=null&&page.getRecords().size()>0){
            for (TeamCourse teamCourse : page.getRecords()) {
                //对象转换
                CoachTeamCourseDTO coachTeamCourseDTO = BeanUtil.toBean(teamCourse, CoachTeamCourseDTO.class);
                //场馆名称赋值
                Result<BusinessStoreDTO> businessStoreDTOResult = businessClient.selByBusinessStoreId(teamCourse.getStoreId());
                if (businessStoreDTOResult.getCode()!=200) throw new CourseException(businessStoreDTOResult.getCode(),businessStoreDTOResult.getMessage());
                //获取数据
                BusinessStoreDTO businessStoreDTO = businessStoreDTOResult.getData();
                coachTeamCourseDTO.setStoreName(businessStoreDTO.getStoreName());
                //教练名称
                Result<Coach> coachResult = coachClient.getCoachById(teamCourse.getCoachId());
                if (coachResult.getCode()!=200) throw new CourseException(coachResult.getCode(),coachResult.getMessage());
                //获取数据
                Coach coach = coachResult.getData();
                //判断是否存在
                if (coach==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_COACH_NOT_EXIST);
                coachTeamCourseDTO.setCoachName(coach.getCoachName());
                //教室名称赋值
                Result<StoreClassroomByIdDTO> storeClassroomByIdDTOResult = businessClient.selNormalByStoreClassroomId(teamCourse.getClassroomId());
                if (storeClassroomByIdDTOResult.getCode()!=200) throw new CourseException(storeClassroomByIdDTOResult.getCode(),storeClassroomByIdDTOResult.getMessage());
                //获取数据
                StoreClassroomByIdDTO storeClassroomByIdDTO = storeClassroomByIdDTOResult.getData();
                //判断是否存在
                if (storeClassroomByIdDTO==null) throw new CourseException(CourseExceptionCode.COURSE_EXCEPTION_CODE_CLASS_ROOM_NOT_EXIST);
                coachTeamCourseDTO.setClassroomName(storeClassroomByIdDTO.getClassroomName());
                //状态赋值
                coachTeamCourseDTO.setTeamCourseState(teamCourse.getTeamCourseState()==0?"待开课":teamCourse.getTeamCourseState()==1?"进行中":"已完成");
                //保存到集合中
                coachTeamCourseDTOS.add(coachTeamCourseDTO);
            }
        }
        //分页对象转换
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(coachTeamCourseDTOS);
        return pageInfo;
    }
}
